package great.project.config;

import great.project.interceptor.OneInterceptor;
import great.project.interceptor.TwoInterceptor;
import great.project.service.i.MobileMessageService;
import great.project.service.impl.OneMobileMessageService;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Component("customWebConfig")
public class WebConfig implements WebMvcConfigurer {

    /**
     * <div  style="height:10px;width:200px;font-size:15px;text-align:center;color:red;">
     * <p>
     *  oneInterceptor
     *
     *  </div>
     */
    @Resource
    private OneInterceptor oneInterceptor;

    /**
     * <div  style="height:10px;width:200px;font-size:15px;text-align:center;color:red;">
     * <p>
     *  twoInterceptor
     *
     *  </div>
     */
    @Resource
    private TwoInterceptor twoInterceptor;

    @Override
    public void addInterceptors(@NonNull InterceptorRegistry registry) {

        registry.addInterceptor(oneInterceptor)
                .addPathPatterns("/test1/**")
                .order(1);

        registry.addInterceptor(twoInterceptor)
                .addPathPatterns("/test2/**")
                .order(0);


    }


    @Override
    public void addCorsMappings(@NonNull CorsRegistry registry) {
        registry.addMapping("/test1/**")
                // 允许的目标源
                .allowedOrigins("localhost")
                // 允许cookie跨域
                .allowCredentials(true)
                // 支持的方法类型
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                // 允许的请求头
                .allowedHeaders("Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization", "sign", "timestamp", "uuid", "token")
                // OPTIONS 请求失效时间，单位为秒
                .maxAge(3600);
    }


    /**
     *  配置短信服务
     * @return 将要使用的短信服务
     */
    @Bean
    public MobileMessageService mobileMessageService(){
        return new OneMobileMessageService();
    }

}
